package com.LeeCode;

import java.util.*;

/**
 * 根据描述创建二叉树
 */

public class Code2196 {
    public static void main(String[] args) {
        int[][] descriptions = {
                {20, 15, 1},
                {20, 17, 0},
                {50, 20, 1},
                {50, 80, 0},
                {80, 19, 1}
        };

        new Code2196().createBinaryTree(descriptions);
    }

    public TreeNode createBinaryTree(int[][] descriptions) {
        Map<Integer, TreeNode> nodes = new HashMap<>();
        Set<Integer> leafs = new HashSet<>();
        for (int[] description : descriptions) {
            int parent = description[0], child = description[1];
            boolean isLeft = description[2] == 1;
            nodes.putIfAbsent(parent, new TreeNode(parent));
            nodes.putIfAbsent(child, new TreeNode(child));
            if (isLeft) {
                nodes.get(parent).left = nodes.get(child);
            } else {
                nodes.get(parent).right = nodes.get(child);
            }
            leafs.add(child);
        }
        for (int val : nodes.keySet()) {
            if (!leafs.contains(val)) {
                return nodes.get(val);
            }
        }
        return null;
    }
}
